from typing import Any, Coroutine

from fastapi import FastAPI, APIRouter, Query, Path
from starlette.responses import JSONResponse
from starlette.staticfiles import StaticFiles

from api import http_api
from entity.parameters import BusinessParametersGoods
from app.utils.response_util import ResponseUtil

app = FastAPI()
router = APIRouter(prefix="/api", tags=["api"])

@router.get("/", summary="API根路径", description="返回API服务的欢迎信息")
async def root():
    return {"message": "Hello World"}

# 查询平台货品接口
@router.get("/goods/infer",summary="测试接口",description="接口调用数据")
async def external_api(
        infer: str = Query(..., min_length=1, description="查询关键词"),
        start_time: str = Query(None, description="开始时间"),
        end_time: str = Query(None, description="开始时间"),
        page_no: int = Query(None, description="开始时间"),
        page_size: int = Query(None, description="开始时间"),
        # 其他必要参数...
):
    print(f"======================  测试接口php的api访问  ======{infer}========================")
    # 创建 BusinessParametersGoods 实例并传递参数
    goods = BusinessParametersGoods(start_time=start_time, end_time=end_time, page_no=page_no, page_size=page_size)
    result = await http_api.call_external_api(infer, goods)
    # 调用外部 API 并返回结果
    return JSONResponse(result)

@router.get("/goods/list",  summary="测试接口", description="接口调用数据")
async def goods_list(
    sortType: str = Query(None, description="<UNK>"),
    sortPrice: str = Query(None, description="<UNK>"),
    categoryId: str = Query(None, description="<UNK>"),
    goodsName:str = Query(None, description="<UNK>"),
    page_no: int = Query(1,ge=1,description="页码，从 1 开始"),
    page_size: int = Query(10,ge=1,le=100,description="页面大小，每页记录数不超过 100")
) -> dict:
    print("===========================接口查询开始=======================================")
    result = await http_api.call_goods_api(page_no, page_size,goodsName)
    count = await http_api.get_goods_count()
    # 正确语法：键名用双引号，逗号分隔各键值对
    data = {
        "total": count,
        "per_page": 15,
        "current_page": page_no,
        "last_page": page_size,
        "data": result
    }


    return ResponseUtil.success(
                        data = data,
                        message = "获取商品列表成功"
                    )

@router.get("/category/list")
async def goods_class_list()-> dict:
    result = await http_api.call_goods_brand()
    count = await http_api.get_goods_brand_count()
    return ResponseUtil.success(
        data={
            "total": count,
            "per_page": 15,
            "data": result
        },
        message="品牌列表获取成功"

    )
@router.get("/setting/data")
async def setting_data()-> dict:
    return ResponseUtil.success(
        data= {         
                "setting": {
                    "page_category_template": {
                        "style": 20,
                        "shareTitle": ""
                    },
                    "points": {
                        "points_name": "积分",
                        "describe": """
                        a) 积分不可兑现、不可转让,仅可在本平台使用;\n
                        b) 您在本平台参加特定活动也可使用积分,详细使用规则以具体活动时的规则为准;\n
                        c) 积分的数值精确到个位(小数点后全部舍弃,不进行四舍五入)\n
                        d) 买家在完成该笔交易(订单状态为"已签收")后才能得到此笔交易的相应积分,
                            如购买商品参加店铺其他优惠,则优惠的金额部分不享受积分获取;
                        """
                    },
                    "recharge": {
                        "is_entrance": 1,
                        "is_custom": 1,
                        "describe": "1. 账户充值仅限微信在线方式支付，充值金额实时到账；\n2. 账户充值套餐赠送的金额即时到账；\n3. 账户余额有效期：自充值日起至用完即止；\n4. 若有其它疑问，可拨打客服电话400-000-1234"
                    },
                    "register": {
                        "registerMethod": 10,
                        "isManualBind": 1,
                        "isOauthMpweixin": 0,
                        "isOauthWxofficial": 1
                    },
                    "recommended": {
                        "enabled": 1,
                        "style": {
                            "title": "商品推荐",
                            "display": "list",
                            "column": 2,
                            "show": [
                                "goodsName",
                                "goodsPrice",
                                "linePrice",
                                "sellingPoint",
                                "goodsSales"
                            ]
                        }
                    },
                    "_other": {
                        "isEnabledDealer": True ,
                        "h5Url": "http://yoshop2-pro.cn/"
                    }
                }
            })


@router.get("/store/data")
async def store_data()-> dict:
    return ResponseUtil.success(
        total_count=0,
        data=[{
            "class_id": "5",
            "parent_id": "-1",
            "is_leaf": "1",
            "class_name": "PDA",
            "path": "-1,5,",
            "modified": "2018-11-2613:47:34",
            "created": "2018-11-26 13:47:34"
        }],
        message="获取商品列表成功"
    )

@router.get("/page/detail")
async def page_detail()-> dict:
    return ResponseUtil.success(
        data={
                "pageData": {
                    "page": {
                        "params": {
                            "name": "页面名称",
                            "title": "首页",
                            "shareTitle": "分享标题"
                        },
                        "style": {
                            "titleTextColor": "black",
                            "titleBackgroundColor": "#ffffff"
                        }
                    },
                    "items": [
                        {
                            "name": "搜索框",
                            "type": "search",
                            "params": {
                                "placeholder": "请输入关键字进行搜索"
                            },
                            "style": {
                                "textAlign": "left",
                                "searchStyle": "square"
                            }
                        }
                    ]
                }
        }
    )
app.include_router(router)

app.mount("/static", StaticFiles(directory="static"), name="static")
